from concept_lattice import ConceptLattice, Concept
from formalContext import FormalContext, read_formal_context
from fastAddIntent import FastAddIntent


def create_lattice_incrementally(formal_context):
    lattice = ConceptLattice()

    # 创建初始概念，空的意图
    top_concept = Concept(intent=[])
    lattice.add_concept(top_concept)

    # 逐步将属性添加到概念格
    for i in range(formal_context.num_attributes):
        # 计算属性i的意图（所有拥有该属性的对象的集合）
        intent = [j for j in range(formal_context.num_objects) if formal_context.get_object(j)[i] == 1]

        # 调用 FastAddIntent 来添加新概念
        FastAddIntent(intent, top_concept, lattice, n=i)

    return lattice
def print_formal_context(context):
    print("Number of objects: {}".format(context.num_objects))
    print("Number of attributes: {}".format(context.num_attributes))
    print("Formal Context Matrix:")
    for row in context.context:
        print(row)
if __name__ == "__main__":
    # 假设形式背景文件名为 'context.txt'
    context_file = '../txt/demo1.txt'

    # 读取形式背景
    formal_context = read_formal_context(context_file)

    # 打印读取的形式背景
    print_formal_context(formal_context)

    # 生成概念格
    lattice = create_lattice_incrementally(formal_context)

    # 打印生成的概念格
    print("Generated Concept Lattice:")
    print(lattice)
